<template>
  <el-form ref="userRef" :model="form" :rules="rules" label-width="100px">
    <el-form-item label="用户邮箱：" prop="email">
      <el-input v-model="form.email" maxlength="100 px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="handleSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { defineProps, ref } from "vue";
import requestUtil from "@/util/request";
import { ElMessage } from 'element-plus'
import { useStore } from "@/store/piniaIndex"

const userStore = useStore();

const form = ref(userStore.GET_USERINFO())

const userRef = ref(null)


const rules = ref({
  email: [{ required: true, message: "邮箱地址不能为空", trigger: "blur" }, { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
});

const handleSubmit = () => {

  userRef.value.validate(async (valid) => {
    if (valid) {
      let result = await requestUtil.post("/user/updateEmail", form.value);
      let data = result.data;
      if (data.code == 1) {
        ElMessage.success("执行成功！")
        userStore.SET_USERINFO(form.value)
      }
    }
  })
}


</script>

<style lang="scss" scoped></style>